package com.zhangkunhao.notes.web.servlet;

import com.alibaba.fastjson.JSON;
import com.zhangkunhao.notes.entity.Do.PermissionBlacklist;
import com.zhangkunhao.notes.entity.Do.Question;
import com.zhangkunhao.notes.entity.Do.Topic;
import com.zhangkunhao.notes.entity.Do.TopicPursueList;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.entity.bo.UserPermissionPage;
import com.zhangkunhao.notes.service.PermissionBlacklistServiceImpl;
import com.zhangkunhao.notes.service.TopicPursueListServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 评论、点赞、提出问题、回答权限拉黑列表的controller
 *
 * @author 20642
 */
@WebServlet("/permissionBlacklist/*")
public class PermissionBlacklistServlet extends BaseDaoServlet {

    private PermissionBlacklistServiceImpl permissionBlacklistService = new PermissionBlacklistServiceImpl();

    public void setUserPrivilege(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据
        String params = request.getReader().readLine();

        //2.转为对象
        PermissionBlacklist permissionBlacklist = JSON.parseObject(params, PermissionBlacklist.class);

        //3.调用service层,封装返回前端的对象
        Message<PermissionBlacklist> message = permissionBlacklistService.setUserPrivilege(permissionBlacklist);

        //4.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(message));

    }

    public void cancelSetUserPrivilege(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收数据
        int losePermissionId = Integer.parseInt(request.getParameter("losePermissionId"));

        //3.调用service层,封装返回前端的对象
        Message<PermissionBlacklist> message = permissionBlacklistService.cancelSetUserPrivilege(losePermissionId);

        //3.写数据,转为json
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(message));
    }

    public void listUserPrivileges(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //调用service
        UserPermissionPage userPermissionPage = permissionBlacklistService.listUserPrivileges();

        // 转换为json数据,写数据
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(userPermissionPage));
    }
}
